package com.lixiaoyi.ems.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-05-17
 */

public class DbUtils {

    //1.获取连接池
    private static DruidDataSource ds;
    //2.线程副本 保证同一个线程 获取到的conn是同一个
    private static ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();

    //3.必要的
    public static DataSource getDs(){
        return ds;
    }
    //代码块
    static {
        //1.创建properties对象
        Properties properties = new Properties();
        //2.字节流
        InputStream is = DbUtils.class.getResourceAsStream("/database.properties");


        try {
            //3.加载properties
            properties.load(is);
            //4.初始化ds
            ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 创建连接
     * @return
     */
    public static Connection getConnection(){
        //1.从当前副本中获取connection
        Connection connection = THREAD_LOCAL.get();
        try {
            //2.若空
            if (connection == null){
                //重新创建
                connection = ds.getConnection();
                //放入线程副本
                THREAD_LOCAL.set(connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 开启事务
     */
    public static void begin(){
        //1.初始化connection
        Connection connection = null;
        try {
            //2.获取connection
            connection = getConnection();
            //3.关闭自动提交
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 提交事务
     */
    public static void commit(){
        Connection connection = null;
        try {
            connection = getConnection();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(connection,null,null);
        }
    }

    /**
     * 回滚事务
     */
    public static void rollback(){
        Connection connection = null;
        try {
            connection = getConnection();
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(connection,null,null);
        }
    }

    /**
     * 关闭所有
     * @param connection
     * @param statement
     * @param resultSet
     */
    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){

        try {
            if (resultSet != null){
                resultSet.close();
            }
            if (statement != null){
                statement.close();
            }
            if (connection != null){
                connection.close();
                //并从线程池中移除
                THREAD_LOCAL.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
